package cn.iocoder.yudao.module.mqtt.framework.web.config;


import cn.iocoder.yudao.module.mqtt.framework.config.MqttProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;

@Slf4j
@Configuration
public class ResourcesConfig implements WebMvcConfigurer {

    @Autowired
    private MqttProperties mqttProperties;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String uploadPath = mqttProperties.getUpload();
        
        // 确保路径格式正确，避免Spring将其作为URL处理
        String resourceLocation;
        
        if (uploadPath == null || uploadPath.trim().isEmpty()) {
            log.error("上传路径配置为空，使用默认路径");
            resourceLocation = "file:/tmp/upload";
        } else {
            uploadPath = uploadPath.trim();
            
            if (uploadPath.startsWith("/")) {
                // Unix/Linux绝对路径，直接使用
                resourceLocation = "file:" + uploadPath;
                log.info("检测到Unix绝对路径: {}", uploadPath);
            } else if (uploadPath.contains(":\\")) {
                // Windows绝对路径，直接使用
                resourceLocation = "file:" + uploadPath;
                log.info("检测到Windows绝对路径: {}", uploadPath);
            } else if (uploadPath.contains("://")) {
                // 已经是完整URL格式，直接使用
                resourceLocation = uploadPath;
                log.info("检测到完整URL路径: {}", uploadPath);
            } else {
                // 相对路径，转换为绝对路径
                File uploadDir = new File(uploadPath);
                if (!uploadDir.isAbsolute()) {
                    // 如果是相对路径，转换为绝对路径
                    uploadDir = new File(System.getProperty("user.dir"), uploadPath);
                }
                resourceLocation = "file:" + uploadDir.getAbsolutePath();
                log.info("检测到相对路径，转换为绝对路径: {} -> {}", uploadPath, uploadDir.getAbsolutePath());
            }
        }
        
        log.info("=== 资源路径配置信息 ===");
        log.info("原始配置路径: {}", uploadPath);
        log.info("解析后路径: {}", resourceLocation);
        log.info("操作系统: {}", System.getProperty("os.name"));
        log.info("用户目录: {}", System.getProperty("user.dir"));
        log.info("========================");
        
        /** 通过url访问项目外的目录图片*/
        registry.addResourceHandler("/cert/**").addResourceLocations(resourceLocation);
        
        // 验证路径是否存在
        try {
            if (resourceLocation.startsWith("file:")) {
                String filePath = resourceLocation.substring(5); // 移除 "file:" 前缀
                File dir = new File(filePath);
                if (dir.exists()) {
                    log.info("资源目录存在: {}", dir.getAbsolutePath());
                } else {
                    log.warn("资源目录不存在，将尝试创建: {}", dir.getAbsolutePath());
                    if (dir.mkdirs()) {
                        log.info("成功创建资源目录: {}", dir.getAbsolutePath());
                    } else {
                        log.error("无法创建资源目录: {}", dir.getAbsolutePath());
                    }
                }
            }
        } catch (Exception e) {
            log.error("验证资源目录时发生异常", e);
        }
    }

}